package ai.assist.common.context;

import ai.assist.common.businesss.mongo.entity.SysDept;
import ai.assist.common.utils.CachedBeanCopier;
import ai.assist.common.businesss.mongo.entity.SysPermission;
import ai.assist.common.businesss.mongo.entity.SysUser;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import org.apache.commons.lang3.StringUtils;

import java.io.Serializable;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @author hui
 * @Date 2021/12/14 下午5:54
 **/
@Data
@ApiModel
public class UserInfoContext implements Serializable {

    @ApiModelProperty(value = "id")
    private String id;

    @ApiModelProperty(value = "手机号")
    private String phone;

    @ApiModelProperty(value = "用户昵称")
    private String nickname;

    private String username;

    private String realName;

    private String email;

    private Integer status;

    private Integer sex;

    private Long deptId;

    private Long deptNo;

    @ApiModelProperty(value = "用户角色")
    private List<String> roles;

    @ApiModelProperty(value = "部门信息")
    private SysDept dept;

    @ApiModelProperty(value = "权限标识")
    private Set<String> permissions;

    @ApiModelProperty("包含部门编码列表(从小到大)")
    private List<Long> deptNos;

    @ApiModelProperty
    private Long parentNo;

    @ApiModelProperty
    private Long unitNo;

//    @ApiModelProperty("回复工单自动审核:1上级已授权,2上级未授权")
//    private Integer autoSendBackAudit;
//
//    @ApiModelProperty("退回自动审核1:上级已授权,2上级未授权")
//    private Integer autoReturnAudit;

    @ApiModelProperty("市级系统账号")
    private String cityUsername;

    @ApiModelProperty("市级系统密码")
    private String cityPassword;

    public String getRealName() {
        if (StringUtils.isNoneBlank(realName)) {
            return realName;
        }
        return username;
    }

    public UserInfoContext() {

    }

    public static UserInfoContext create(SysUser user, SysDept dept, List<String> roles, List<SysPermission> permissions) {
        UserInfoContext userinfo = new UserInfoContext();
        CachedBeanCopier.copyObject(user, userinfo);
        userinfo.setId(user.getId());
        userinfo.setRoles(roles);
        userinfo.setDept(dept);
        userinfo.setDeptNo(dept != null ? dept.getDeptNo() : null);
        List<String> deptNo = Arrays.asList(dept.getDeptPath().split("_"));
        List<Long> deptPath = new ArrayList<>();
        userinfo.setParentNo(deptNo.size() < 2 ? 0 : Long.valueOf(deptNo.get(deptNo.size() - 2)));

        for (String s : deptNo) {
            deptPath.add(Long.valueOf(s));
        }

        if (dept.getDeptLevel() <= 2) {
            userinfo.setUnitNo(dept.getDeptNo());
        } else {
            if (deptNo.get(1).equals("304060")) {
                userinfo.setUnitNo(Long.valueOf(deptNo.get(2)));
            } else if (deptNo.get(0).equals("304") || deptNo.get(0).equals("100100")) {
                userinfo.setUnitNo(Long.valueOf(deptNo.get(1)));
            } else {
                userinfo.setUnitNo(Long.valueOf(deptNo.get(2)));
            }
        }
        userinfo.setDeptNos(deptPath);
        Set<String> permission = permissions.stream().filter(i -> !StringUtils.isEmpty(i.getPerms())).map(SysPermission::getPerms).collect(Collectors.toSet());
        Set<String> permis = new HashSet<>();
        permission.forEach(i -> {
            List<String> p = Arrays.asList(i.split(","));
            p.forEach(j -> {
                permis.add(j);
            });
        });
        userinfo.setPermissions(permis);
        return userinfo;
    }

}
